package app.agent.scatchpad

import app.Mode
import app.agent.scatchpad.ScratchPadLocator._
import app.comet.{RequestServices, Subscriber}
import app.server.ServicesResponse
import app.ui.widget.{ExpandingToggle, ExpandingToggleParent}
import im.mange.jetboot.js.JsCmdFactory
import im.mange.jetboot.{R, Renderable, Html}
import JsCmdFactory._
import Html._

//TODO: slide in the content
case class ScratchPadAgent(subscriber: Subscriber) extends Renderable {
  private val contentAgent = ScratchPadContentAgent()
  private val content = span(R("Scratch pad"))

  private val toggle = ExpandingToggle(scratchPadToggle, content, "list-alt", new ExpandingToggleParent {
    override def onShow = showScratchPad
    override def onHide = contentAgent.onHide //TODO: this looks common
  })

  override def render = div(id = scratchPadAgent, toggle, contentAgent).classes(if (Mode.isDemo) "hidden" else "").render

  //TODO: obviously this needs a ScratchPadResponse
  def onShowResponse(response: ServicesResponse) = toggle.stopSpinner & contentAgent.onShowResponse(response)

  //TODO: should this go upto parent.xyz instead?
  private[agent] def showScratchPad = {
    subscriber ! RequestServices
    nothing
  }
}
